package com.zyt.controller;

import com.zyt.entity.Brand;
import com.zyt.entity.Phone;
import com.zyt.service.BrandService;
import com.zyt.service.PhoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class PhoneController {
    @Autowired
    private PhoneService phoneService;
    @Autowired
    private BrandService brandService;
    @RequestMapping("/selectAllPhone")
    public String selectAllPhone(Model model){
        List<Phone> phones = phoneService.selectAllPhone();
        System.out.println(phones);
        model.addAttribute("phones",phones);
        return "phone-list.jsp";
    }
    @RequestMapping("/insertAllPhone")
    public String insertAllPhone(Phone phone){
        phoneService.insertPhone(phone);
        return "/selectAllPhone.do";
    }

    @RequestMapping("/deleteById")
    public String deleteById(Integer id){
        phoneService.deleteById(id);
        return "/selectAllPhone.do";
    }
    @RequestMapping("/selectById")
    public String selectById(Integer id,Model model){
        Phone phone = phoneService.selectByPhone(id);
        List<Brand> brands = brandService.selectAllBrand();
        model.addAttribute("brands1",brands);
        model.addAttribute("phone1",phone);
        return "/updatephone.jsp";
    }
    @RequestMapping("/updateById")
    public String updateById(Phone phone){
        phoneService.updateById(phone);
        return "/selectAllPhone.do";
    }
    @RequestMapping("/deleteAll")
    public String deleteAll(Integer[] ids){
        for (int i=0;i<ids.length;i++){
            phoneService.deleteById(ids[i]);
        }
        return "/selectAllPhone.do";
    }
}
